/**
 * @description: 定义班级 service
 * @version: 0.0.1
 * @author: z..
 * @Date: 2023-06-12 18:16:39
 */

import { Provide } from '@midwayjs/core';
import { JsonConfig } from './json_config.entity';

import { InjectRepository, InjectDataSource } from '@midwayjs/sequelize';
import { Repository, Sequelize } from 'sequelize-typescript';

@Provide()
export class JsonConfigModel {

  @InjectRepository(JsonConfig)
  JsonConfigRepository: Repository<JsonConfig>;

  @InjectDataSource()
  SequelizeQuery: Sequelize;

  /**
   * 查询当前编码的记录条数
   * @param {*} jsonCode
   * @returns 
   */
  async getCountByCode(jsonCode: string): Promise<any> {
    const sql = `SELECT count(1) as num FROM json_config WHERE json_code = '${jsonCode}';`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }


  /**
   * 根据应用名称查询配置数据
   * @param {*} jsonCode
   * @returns 
   */
  async getDataByCode(jsonCode: string): Promise<any> {
    const sql = `SELECT id,json_name,json_code,json_data,created_at,updated_at FROM json_config WHERE json_code = '${jsonCode}';`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
    * 根据id更新数据
    * @param {*} id 
    * @param {*} jsonData
    * @returns 
    */
  async updateDateById({ id, jsonData }: { id: number, jsonData: string }): Promise<any> {
    return await this.JsonConfigRepository.update(
      {
        json_data: jsonData,
        updated_at: new Date()
      },
      {
        where: { id: id },
      }
    );
  }

  /**
    * 根据code更新数据
    * @param {*} jsonCode 
    * @param {*} jsonData
    * @returns 
    */
  async updateDateByCode({ jsonCode, jsonData }: { jsonCode: string, jsonData: string }): Promise<any> {
    return await this.JsonConfigRepository.update(
      {
        json_data: jsonData,
        updated_at: new Date()
      },
      {
        where: { json_code: jsonCode }
      }
    )
  }

  /**
    * 新增json数据
    * @param {*} jsonCode 
    * @param {*} jsonName 
    * @param {*} jsonData
    * @returns 
    */
  async insertDataInfo({ jsonCode, jsonName, jsonData }: { jsonCode: string, jsonName: string, jsonData: string }): Promise<any> {
    const sql = `INSERT INTO json_config 
    SET json_code = '${jsonCode}',
        json_name = '${jsonName}',
        json_data = '${jsonData}',
        created_at = now(),
        updated_at = now();`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
   * 分页查询获取总条数
   * @param {*} searchKey   搜索信息
   * @returns 
   */
  async getTotalNum(searchKey: any): Promise<any> {
    let sql = `SELECT count(1) as num from json_config;`;
    if (searchKey) {
      sql = `SELECT count(1) AS num FROM json_config WHERE json_name LIKE '%${searchKey}%';`;
    }
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
   * 分页查询获取列表
   * @param {*} pageNum 
   * @param {*} pageSize 
   * @param {*} searchKey 
   * @returns 
   */
  async getDataByPage(pageNum: number, pageSize: number, searchKey: any): Promise<any> {
    const _limit = (pageNum - 1) * pageSize;
    let sql = `SELECT id,json_code,json_name,json_data,created_at,updated_at FROM json_config ORDER BY created_at DESC LIMIT ${_limit},${pageSize};`;

    if (searchKey) {
      sql = `SELECT id,json_code,json_name,json_data,created_at,updated_at FROM json_config WHERE json_name LIKE '%${searchKey}%' ORDER BY created_at DESC LIMIT ${_limit},${pageSize};`;
    }
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

}